package com.zhongjian.technologycal.mall.activity.personal.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.bean.BaseBean;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.StatusBarUtil;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.UserUtils;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/12/14.
 * 设置昵称的页面实现
 */

public class SettingNickNameActivity extends BaseActivity {
    @BindView(R.id.tv_main_title)
    TextView tv_main_title;
    @BindView(R.id.input_et_nickname)
    EditText input_et_nickname;
    private String nickName;

    @Override
    protected void beforeInit() {
        super.beforeInit();
//        StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.border_color));
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, true);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_main_title.setText("设置昵称");
        nickName = getIntent().getStringExtra("nickName");
        if (TextUtils.isEmpty(nickName) || nickName == null) {
            input_et_nickname.setHint("请输入昵称");
        } else {
            input_et_nickname.setText("" + nickName);
        }
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_setting_nickname;
    }

    @OnClick(R.id.iv_back_page)
    public void onClickBack() {
        finish();
    }

    @Override
    public void initPresenter() {

    }

    @OnClick(R.id.commit_tv_nickname)
    public void onClickNickname() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        } else if (TextUtils.isEmpty(input_et_nickname.getText().toString().trim())) {
            ToastUitl.showShort("昵称不能为空～");
            return;

        }
        HttpRetrofitClient.getInstance(getBaseContext()).postUrlRequestNickname(UserUtils.getUserToken(),
                input_et_nickname.getText().toString().trim(), new Subscriber<ResponseBody>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        Gson gson = new Gson();
                        try {
                            BaseBean baseBean = gson.fromJson(responseBody.string(), BaseBean.class);
                            if (baseBean.getError_code() == 0) {
                                ToastUitl.showShort("设置成功～");
                                finish();
                            } else if (baseBean.getError_code() == 3) {
                                Intent intent = new Intent();
                                intent.setAction("com.broadcast.info_out_line");
                                sendBroadcast(intent);
                            } else {
                                ToastUitl.showShort("设置失败～");
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }


                    }
                });
    }

}




